﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using WB.Library;
using WB.Entity;
using WB.DaoHelper;
public partial class Admin_Product_AddEditCat : System.Web.UI.UserControl
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadCombo();
            LoadData();
        }
    }

    void LoadCombo()
    {
        ddlCat.DataSource = CategoryDao.Instants().GetAllParent();
        ddlCat.DataTextField = "CatName";
        ddlCat.DataValueField = "Id";
        ddlCat.DataBind();
    }

    void LoadData()
    {
        rep.DataSource = CategoryDao.Instants().GetAll();
        rep.DataBind();
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        CategoryEntity ob = CategoryDao.Instants().SelectOne(FGuid.ToGuid(hidId.Value));
        CategoryEntity parent = CategoryDao.Instants().SelectOne(FGuid.ToGuid(ddlCat.Text));
        if (ob == null)
        {
            ob = new CategoryEntity();
            ob.CatName = txtCatName.Text.Trim();
            ob.ParentCode = parent.CatCode;
            ob.ParentId = parent.Id;
            ob.CreatedBy = Util.CurrentUserName;
            ob.CreatedDate = DateTime.Now;
            ob.UpdatedBy = Util.CurrentUserName;
            ob.UpdatedDate = DateTime.Now;
            CategoryDao.Instants().Insert(ob);
        }
        else
        {
            ob.CatName = txtCatName.Text.Trim();
            ob.ParentCode = parent.CatCode;
            ob.ParentId = parent.Id;
            ob.UpdatedBy = Util.CurrentUserName;
            ob.UpdatedDate = DateTime.Now;
            CategoryDao.Instants().Update(ob);
        }
        Response.Redirect(Request.RawUrl);
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect(Request.RawUrl);
    }

    protected void rep_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        CategoryEntity ob;
        switch (e.CommandName)
        {
            case "edit":
                ob = CategoryDao.Instants().SelectOne(FGuid.ToGuid(e.CommandArgument.ToString()));
                if (ob != null)
                {
                    ddlCat.Text = ob.ParentId.ToString();
                    txtCatName.Text = ob.CatName;
                    hidId.Value = ob.Id.ToString();
                    btnUpdate.Text = "Cập nhật";
                    btnCancel.Visible = true;
                }
                break;
            case "del":
                if (CategoryDao.Instants().CheckDelete(FGuid.ToGuid(e.CommandArgument.ToString())))
                {
                    ob = CategoryDao.Instants().SelectOne(FGuid.ToGuid(e.CommandArgument.ToString()));
                    if (ob != null)
                    {
                        ob.IsDeleted = true;
                        ob.UpdatedBy = Util.CurrentUserName;
                        ob.UpdatedDate = DateTime.Now;
                        CategoryDao.Instants().Update(ob);
                    }
                    Response.Redirect(Request.RawUrl);
                }
                else
                {
                    CustomValidator1.ErrorMessage = "Danh mục đang sử dụng, không thể xóa";
                    CustomValidator1.IsValid = false;
                }
                break;
        }
    }
}
